package org.xteam.engine;

public class Bresenham {

	public static void draw(int xi, int yi, int xf, int yf, PixelBuffer pixelBuffer) {
		int x = xi;
		int y = yi;
		int dx = xf - xi;
		int dy = yf - yi;
		int xinc = (dx > 0) ? 1 : -1;
		int yinc = (dy > 0) ? 1 : -1;
		dx = Math.abs(dx);
		dy = Math.abs(dy);
		pixelBuffer.putPixel(x, y);
		if (dx > dy) {
			int cumul = dx;
			for (int i = 1; i <= dx; ++i) {
				x += xinc;
				cumul += 2 * dy;
				if (cumul >= 2 * dx) {
					cumul -= 2 * dx;
					y += yinc;
				}
				pixelBuffer.putPixel(x, y);
			}
		} else {
			int cumul = dy;
			for (int i = 1; i <= dy; ++i) {
				y += yinc;
				cumul += 2 * dx;
				if (cumul >= 2 * dy) {
					cumul -= 2 * dy;
					x += xinc;
				}
				pixelBuffer.putPixel(x, y);
			}
		}
	}

}
